/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.google.common.collect.ImmutableSet;
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.Contact.Attachment;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.ParsedContact;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ContactAction
/*     */   extends ItemAction
/*     */ {
/*  49 */   private static final Set<String> CONTACT_OPS = ImmutableSet.of("update");
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException, SoapFaultException
/*     */   {
/*  53 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*     */     
/*  55 */     Element action = request.getElement("action");
/*  56 */     String operation = action.getAttribute("op").toLowerCase();
/*     */     
/*  58 */     if ((operation.endsWith("read")) || (operation.endsWith("spam"))) {
/*  59 */       throw ServiceException.INVALID_REQUEST("invalid operation on contact: " + operation, null);
/*     */     }
/*     */     String successes;
/*     */     String successes;
/*  63 */     if (CONTACT_OPS.contains(operation)) {
/*  64 */       successes = handleContact(context, request, operation);
/*     */     } else {
/*  66 */       successes = handleCommon(context, request, operation, MailItem.Type.CONTACT);
/*     */     }
/*  68 */     Element response = zsc.createElement(MailConstants.CONTACT_ACTION_RESPONSE);
/*  69 */     Element actionOut = response.addUniqueElement("action");
/*  70 */     actionOut.addAttribute("id", successes);
/*  71 */     actionOut.addAttribute("op", operation);
/*  72 */     return response;
/*     */   }
/*     */   
/*     */   private String handleContact(Map<String, Object> context, Element request, String operation) throws ServiceException, SoapFaultException
/*     */   {
/*  77 */     Element action = request.getElement("action");
/*     */     
/*  79 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  80 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  81 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*     */ 
/*  84 */     ArrayList<Integer> local = new ArrayList();
/*  85 */     HashMap<String, StringBuilder> remote = new HashMap();
/*  86 */     partitionItems(zsc, action.getAttribute("id"), local, remote);
/*  87 */     StringBuilder successes = proxyRemoteItems(action, remote, request, context);
/*     */     
/*  89 */     if (!local.isEmpty()) {
/*     */       String localResults;
/*  91 */       if (operation.equals("update"))
/*     */       {
/*  93 */         String folderId = action.getAttribute("l", null);
/*  94 */         ItemId iidFolder = new ItemId(folderId == null ? "-1" : folderId, zsc);
/*  95 */         if (!iidFolder.belongsTo(mbox))
/*  96 */           throw ServiceException.INVALID_REQUEST("cannot move item between mailboxes", null);
/*  97 */         if ((folderId != null) && (iidFolder.getId() <= 0)) {
/*  98 */           throw MailServiceException.NO_SUCH_FOLDER(iidFolder.getId());
/*     */         }
/* 100 */         String flags = action.getAttribute("f", null);
/* 101 */         String[] tags = TagUtil.parseTags(action, mbox, octxt);
/* 102 */         Color color = getColor(action);
/* 103 */         ParsedContact pc = null;
/* 104 */         if (!action.listElements("a").isEmpty()) {
/* 105 */           Contact cn = local.size() == 1 ? mbox.getContactById(octxt, ((Integer)local.get(0)).intValue()) : null;
/* 106 */           Pair<Map<String, Object>, List<Contact.Attachment>> cdata = CreateContact.parseContact(action, zsc, octxt, cn);
/* 107 */           pc = new ParsedContact((Map)cdata.getFirst(), (List)cdata.getSecond());
/*     */         }
/*     */         
/* 110 */         localResults = ContactActionHelper.UPDATE(zsc, octxt, mbox, local, iidFolder, flags, tags, color, pc).getResult();
/*     */       } else {
/* 112 */         throw ServiceException.INVALID_REQUEST("unknown operation: " + operation, null); }
/*     */       String localResults;
/* 114 */       successes.append(successes.length() > 0 ? "," : "").append(localResults);
/*     */     }
/*     */     
/* 117 */     return successes.toString();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/ContactAction.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */